//
// Created by liushuai on 2021/2/22.
//

#include "diameterOfBinaryTree.h"
//int res = 0;
int deepthOfTree(TreeNode *root,int& res) {
    if (!root)
        return 0;
    int leftDepth = deepthOfTree(root->left,res);
    int rightDepth = deepthOfTree(root->right,res);
    res = leftDepth+rightDepth>=res?leftDepth+rightDepth:res;

    return leftDepth>rightDepth?leftDepth + 1:rightDepth+1;
}

int diameterOfBinaryTree(TreeNode *root) {
    if(!root)
        return 0;
    int res = 0;
    deepthOfTree(root,res);
    return res;
}

